//  MineVC.m
//  XJMCustomer
//  Created by Mr.hu on 2017/10/9.
//  Copyright © 2017年 Mr.hu. All rights reserved.

#import "MineVC.h"
#import "MineFirstTBVCell.h"
#import "MineSecondTBVCell.h"
#import "MineThreeTBVCell.h"

#import "NewsRemindVC.h"
#import "MineSettingVC.h"

#import "MineAccountTBV.h"
#import "MineAppraiseTBV.h"
#import "MineAblumVedio.h"

#import "LoginMainVC.h"
#import "CheckTechnicianInfromation.h"
#import "ActivityReward.h"
#import "CustomerServiceCenter.h"
#import "MineTech_homePage.h"

#import "FirstWaves.h"
#import "SecondWaves.h"

#import "AddSomepersonVC.h"
#import "OrderListVC.h"

#import "CurrentServiceVC.h"
#import "InviteCoupon_friends.h"
#import "CashHandFirstTBV.h"

#import "UploadingHeadimgeVC.h"
#import "PersonalModel.h"
@interface MineVC ()<UITableViewDataSource,UITableViewDelegate, MineSettingVCDelegate>
{
    PersonalModel *_personalModel;
}
@property (weak, nonatomic) IBOutlet UITableView *MineTableView;
@property (nonatomic,strong)  MineFirstTBVCell * First_friend;
@property (nonatomic,strong)  NSDictionary * persondict;
@property (strong, nonatomic) FirstWaves *waveView;
/** 注释 **/
@property (strong, nonatomic) SecondWaves *SecondwaveView;

@end

@implementation MineVC

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.tabBarItem.title = @"我的";
        self.navigationItem.title = @"我的";
        self.tabBarItem.image = [[UIImage imageNamed:@"wode"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        self.tabBarItem.selectedImage = [[UIImage imageNamed:@"wodeSelect"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.MineTableView reloadData];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
    dispatch_group_t group =  dispatch_group_create();
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self requestCenterData]; //解决重新进来闪屏下
    });
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        
    });
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    NSInteger value = [[[NSUserDefaults standardUserDefaults] objectForKey:ktabbarChangeValue] integerValue];
    if (value) {
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    }
}

- (void)requestCenterData{
    
    if ([XJToolClass getToken] ) {
        [XJHttpClass teachPersonCenterandFinishblockgetToken:[XJToolClass getToken] andFinishblock:^(id response, NSString *errStr) {
            if (errStr) {
                [XJToolClass tipAnimationWith:errStr];
            }else{
                self.persondict = response[@"data"];
                NSString * headur = self.persondict[@"headurl"];
                if (headur == nil || headur ==NULL || headur.length == 0) {
                    [self.First_friend.MineHeadImage setImage:[UIImage imageNamed:@"touxiang"] forState:UIControlStateNormal];
                }else{
                    [self.First_friend.MineHeadImage sd_setImageWithURL:self.persondict[@"headurl"] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"touxiang"]];
                    [TechSimple registerpass].User_Headimage = self.persondict[@"headurl"];
                    self.First_friend.MineName.text = self.persondict[@"truename"];
                    //收入和总单数
                    self.First_friend.mineInput_price.text = [NSString stringWithFormat:@"¥%@",self.persondict[@"my_income"]];
                    self.First_friend.MyInput_Number.text = self.persondict[@"order_number"];
                    [TechSimple registerpass].User_TechTotalMoney = self.persondict[@"my_income"];
                }
                
                _personalModel = [PersonalModel mj_objectWithKeyValues:response];
                
            }
        }];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
     [self MineTableViewController];
}


- (void)MineTableViewController{
    
    self.MineTableView.delegate = self;
    self.MineTableView.dataSource = self;
    self.MineTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.MineTableView registerNib:[UINib nibWithNibName:@"MineFirstTBVCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"MineFirstTBVCell"];
    
    [self.MineTableView registerNib:[UINib nibWithNibName:@"MineSecondTBVCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"MineSecondTBVCell"];
    [self.MineTableView registerNib:[UINib nibWithNibName:@"MineThreeTBVCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"MineThreeTBVCell"];
    
}

#pragma mark - TableViewDatasource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 9;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        self.First_friend = [tableView dequeueReusableCellWithIdentifier:@"MineFirstTBVCell" forIndexPath:indexPath];
        
        if(self.First_friend == nil){
            self.First_friend = [[MineFirstTBVCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MineFirstTBVCell"];
        }
        
        [self.First_friend.MineMainSetting addTarget:self action:@selector(PersonSettingMeth) forControlEvents:UIControlEventTouchUpInside];
        [self.First_friend.MineNewsRemindBtn addTarget:self action:@selector(NewsReminderMeth) forControlEvents:UIControlEventTouchUpInside];
        self.First_friend.selectionStyle = UITableViewCellSelectionStyleNone;
        self.First_friend.userInteractionEnabled = YES;
        
        NSString *urlhead = self.persondict[@"headurl"];
        if ( urlhead == nil || urlhead == NULL || urlhead.length == 0) {
            [self.First_friend.MineHeadImage setImage:[UIImage imageNamed:@"touxiang"] forState:UIControlStateNormal];
            NSString *name = self.persondict[@"truename"];
            name = name.length ? self.persondict[@"truename"] : @"姓名";
            self.First_friend.MineName.text = name;
        }else{
            
            [self.First_friend.MineHeadImage sd_setImageWithURL:[NSURL URLWithString:self.persondict[@"headurl"]] forState:UIControlStateNormal];
            [TechSimple registerpass].User_Headimage = self.persondict[@"headurl"];
            self.First_friend.MineName.text = self.persondict[@"truename"];
            
            //收入和总单数
            self.First_friend.mineInput_price.text = [NSString stringWithFormat:@"¥%@",self.persondict[@"my_income"]];
            self.First_friend.MyInput_Number.text = self.persondict[@"order_number"];
        }
        
        [self.First_friend.MineHeadImage addTarget:self action:@selector(PersonPageMainMeth) forControlEvents:(UIControlEventTouchUpInside)];
        
        [_waveView removeFromSuperview];
        [_SecondwaveView removeFromSuperview];
        
        self.First_friend.MineHeadImage.layer.masksToBounds = YES;
        self.First_friend.MineHeadImage.layer.cornerRadius = 35;
        _waveView = [[FirstWaves alloc] initWithFrame:CGRectMake(0, 220, ScreenWidth+50, 60)];
        _waveView.alpha = 1;
        [self.First_friend.WaveView_Background addSubview: _waveView];
        _SecondwaveView = [[SecondWaves alloc] initWithFrame:CGRectMake(0, 220, ScreenWidth + 50, 60)];
        _SecondwaveView.alpha = 1;
        self.First_friend.WaveViewStatus.alpha = 0;
        [self.First_friend.WaveView_Background addSubview: _SecondwaveView];
        
        self.First_friend.MineName.text = [XJToolClass getNickname];
        return self.First_friend;
        
    }else if (indexPath.section == 1){
        MineSecondTBVCell * friend=[tableView dequeueReusableCellWithIdentifier:@"MineSecondTBVCell" forIndexPath:indexPath];
        friend.accessoryType  = UITableViewCellAccessoryDisclosureIndicator;
        friend.selectionStyle = UITableViewCellSelectionStyleNone;
        [friend.MineSecondLeftImage setImage:[UIImage imageNamed:@"zhuye"]];
        friend.selectionStyle = UITableViewCellSelectionStyleNone;
        friend.MineSecondName.text = @"我的主页";
        return friend;
    }else if (indexPath.section == 2){
        MineSecondTBVCell * friend=[tableView dequeueReusableCellWithIdentifier:@"MineSecondTBVCell" forIndexPath:indexPath];
        friend.accessoryType  = UITableViewCellAccessoryDisclosureIndicator;
        friend.selectionStyle = UITableViewCellSelectionStyleNone;
        [friend.MineSecondLeftImage setImage:[UIImage imageNamed:@"wodedingdan"]];
        friend.MineSecondName.text = @"我的订单";
        return friend;
        
    }else if (indexPath.section == 3){
        MineSecondTBVCell * friend=[tableView dequeueReusableCellWithIdentifier:@"MineSecondTBVCell" forIndexPath:indexPath];
        friend.accessoryType  = UITableViewCellAccessoryDisclosureIndicator;
        friend.selectionStyle = UITableViewCellSelectionStyleNone;
        [friend.MineSecondLeftImage setImage:[UIImage imageNamed:@"zhanghu"]];
        friend.MineSecondName.text = @"我的账户";
        return friend;
    }else if (indexPath.section == 4){
        MineSecondTBVCell * friend=[tableView dequeueReusableCellWithIdentifier:@"MineSecondTBVCell" forIndexPath:indexPath];
        friend.accessoryType  = UITableViewCellAccessoryDisclosureIndicator;
        friend.selectionStyle = UITableViewCellSelectionStyleNone;
        [friend.MineSecondLeftImage setImage:[UIImage imageNamed:@"pingjia"]];
        friend.MineSecondName.text = @"我的评价";
        return friend;
    }else if (indexPath.section == 5){
        MineSecondTBVCell * friend=[tableView dequeueReusableCellWithIdentifier:@"MineSecondTBVCell" forIndexPath:indexPath];
        friend.accessoryType  = UITableViewCellAccessoryDisclosureIndicator;
        friend.selectionStyle = UITableViewCellSelectionStyleNone;
        [friend.MineSecondLeftImage setImage:[UIImage imageNamed:@"xiangce"]];
        friend.MineSecondName.text = @"我的视频、相册";
        return friend;
    }else if (indexPath.section == 6){
        MineSecondTBVCell * friend=[tableView dequeueReusableCellWithIdentifier:@"MineSecondTBVCell" forIndexPath:indexPath];
        friend.accessoryType  = UITableViewCellAccessoryDisclosureIndicator;
        friend.selectionStyle = UITableViewCellSelectionStyleNone;
        [friend.MineSecondLeftImage setImage:[UIImage imageNamed:@"jiangli"]];
        friend.MineSecondName.text = @"活动奖励";
        return friend;
    }else if(indexPath.section == 7) {
        MineSecondTBVCell * friend=[tableView dequeueReusableCellWithIdentifier:@"MineSecondTBVCell" forIndexPath:indexPath];
        friend.accessoryType  = UITableViewCellAccessoryDisclosureIndicator;
        friend.selectionStyle = UITableViewCellSelectionStyleNone;
        [friend.MineSecondLeftImage setImage:[UIImage imageNamed:@"kezhao"]];
        friend.MineSecondName.text = @"客服中心";
        return friend;
    }else{
        MineSecondTBVCell * friend=[tableView dequeueReusableCellWithIdentifier:@"MineSecondTBVCell" forIndexPath:indexPath];
        friend.accessoryType  = UITableViewCellAccessoryDisclosureIndicator;
        friend.selectionStyle = UITableViewCellSelectionStyleNone;
        [friend.MineSecondLeftImage setImage:[UIImage imageNamed:@"iconyao"]];
        friend.MineSecondName.text = @"邀请好友";
        return friend;
    }
}

- (void)PersonPageMainMeth{
    if ([XJToolClass getUserId]) {
        CheckTechnicianInfromation * minecollect = [[CheckTechnicianInfromation alloc] init];
        [self.navigationController pushViewController:minecollect animated:YES];
    }else{
        LoginMainVC * setting = [[LoginMainVC alloc] init];
        [self.navigationController pushViewController:setting animated:YES];
    }
}

#pragma mark TableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 340;
    }else{
        return 50;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 5) {
        return 8;
    }else{
        return 0;
    }
}

//显示中间线
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    return @" ";
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    if (section == 5) {
        UIView * footview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 8)];
        footview.backgroundColor = TableViewCellFootColor;
        self.MineTableView.tableFooterView = footview;
        return footview;
    }else{
        return 0;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0) {
        
    }else if (indexPath.section == 1){
        
        if ([XJToolClass getUserId]) {
            CheckTechnicianInfromation * minecollect = [[CheckTechnicianInfromation alloc]init];
            minecollect.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:minecollect animated:YES];
        }else{
            LoginMainVC * setting = [[LoginMainVC alloc]init];
            setting.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:setting animated:YES];
        }
        
    }else if (indexPath.section == 2){
        
        if ([XJToolClass getUserId]) {
            OrderListVC * setting = [[OrderListVC alloc]init];
            setting.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:setting animated:YES];
        }else{
            LoginMainVC * setting = [[LoginMainVC alloc]init];
            setting.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:setting animated:YES];
        }
        
    }else if (indexPath.section == 3){
        
        if ([XJToolClass getUserId]) {
            MineAccountTBV * minecollect = [[MineAccountTBV alloc]init];
            minecollect.Input_price = self.persondict[@"my_income"];
            minecollect.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:minecollect animated:YES];
        }else{
            LoginMainVC * setting = [[LoginMainVC alloc]init];
            setting.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:setting animated:YES];
        }
        
    }else if (indexPath.section == 4){
        if ([XJToolClass getUserId]) {
            MineAppraiseTBV * minecollect = [[MineAppraiseTBV alloc]init];
            minecollect.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:minecollect animated:YES];
        }else{
            LoginMainVC * setting = [[LoginMainVC alloc] init];
            setting.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:setting animated:YES];
        }
        
    }else if (indexPath.section == 5){
        if ([XJToolClass getUserId]) {
            MineAblumVedio * minecollect = [[MineAblumVedio alloc]init];
            minecollect.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:minecollect animated:YES];
        }else{
            LoginMainVC * setting = [[LoginMainVC alloc]init];
            setting.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:setting animated:YES];
        }
        
    }else if (indexPath.section == 6){
        
        if ([XJToolClass getUserId]) {
            ActivityReward * minecollect = [[ActivityReward alloc]init];
            minecollect.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:minecollect animated:YES];
        }else{
            LoginMainVC * setting = [[LoginMainVC alloc]init];
            setting.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:setting animated:YES];
        }
        
    }else if (indexPath.section == 7){
     
        if ([XJToolClass getUserId]) {
            CustomerServiceCenter * minecollect = [[CustomerServiceCenter alloc]init];
            minecollect.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:minecollect animated:YES];
        }else{
            LoginMainVC * setting = [[LoginMainVC alloc]init];
            setting.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:setting animated:YES];
        }
        
    }else{
        
        if ([XJToolClass getUserId]) {
            InviteCoupon_friends * minecollect = [[InviteCoupon_friends alloc]init];
            minecollect.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:minecollect animated:YES];
        }else{
            LoginMainVC * setting = [[LoginMainVC alloc]init];
            setting.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:setting animated:YES];
        }
    }
}

- (void)PersonSettingMeth{
    
    if ([XJToolClass getUserId]) {
        MineSettingVC * setting = [[MineSettingVC alloc]initWithSkil:_personalModel.data.desc];
        setting.delegate = self;
        setting.head_URLStr = self.persondict[@"headurl"];
        [self.navigationController pushViewController:setting animated:YES];
    }else{
        LoginMainVC * setting = [[LoginMainVC alloc]init];
        [self.navigationController pushViewController:setting animated:YES];
    }
}

- (void)logoutWithController:(MineSettingVC *)controller
{
    self.persondict = nil;
    [self.MineTableView reloadData];
}

- (void)NewsReminderMeth{
    
    if ([XJToolClass getUserId]) {
        
        NewsRemindVC * setting = [[NewsRemindVC alloc]init];
        setting.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:setting animated:YES];
        
    }else{
                
       LoginMainVC * setting = [[LoginMainVC alloc]init];
       setting.hidesBottomBarWhenPushed = YES;
       [self.navigationController pushViewController:setting animated:YES];
    }
}

@end
